package com.baofu.paycore.service.processor.transfer;

import com.baofu.clear.service.facade.enums.EventTypeEnum;
import com.baofu.paycore.biz.external.clear.EventService;
import com.baofu.paycore.biz.external.clear.model.EventRuleResBO;
import com.baofu.paycore.common.enums.FlagEnum;
import com.baofu.paycore.common.enums.FundTypeEnum;
import com.baofu.paycore.common.utils.Constant;
import com.baofu.paycore.dal.model.SubTransferDO;
import com.baofu.paycore.manager.model.TransferBO;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.model.transfer.CancelTransferDTO;
import com.baofu.paycore.service.facade.model.transfer.TransferDTO;
import com.baofu.paycore.service.facade.model.transfer.TransferFundDTO;
import com.baofu.paycore.service.facade.model.transfer.UnfreezeTransferDTO;
import com.baofu.paycore.service.processor.PayBaseProcessor;
import com.system.commons.exception.BizServiceException;
import com.system.commons.sequence.redis.SequenceFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * </p>
 * User:ames wang  Date: 2017/6/28
 */
@Slf4j
@Component
public class UnfreezeTransferFundProcessor implements PayBaseProcessor<TransferBO,UnfreezeTransferDTO> {

    /**
     * 资金单明细NO
     */
    @Autowired
    private SequenceFacade sequenceFacade;

    /**
     * 会计事件服务
     */
    @Autowired
    private EventService eventService;


    /**
     * 子对象生成
     * @param payBaseBO     支付模型
     * @param payBaseDTO    请求参数
     * @return
     */
    @Override
    public PayBaseProcessor process(TransferBO payBaseBO, UnfreezeTransferDTO payBaseDTO) {
        List<SubTransferDO> subTransferDOs = new ArrayList<>();
        if( Constant.validateLong(payBaseDTO.getBizFeeAmount())>0){
            SubTransferDO subTransferDOByFee =
                    this.getSubTransfer(payBaseBO.getPayNo(),payBaseDTO,
                            Constant.validateLong(payBaseDTO.getBizFeeAmount()),
                            null, payBaseDTO.getOperator(),
                            payBaseDTO.getCcy());
            subTransferDOByFee.setEventNo(getEventNo(EventTypeEnum.FEE, payBaseDTO.getSubBizType()));
            subTransferDOByFee.setFundType(FundTypeEnum.FEE.getCode());
            subTransferDOByFee.setOutAccountNo(payBaseDTO.getAccountNo());

            subTransferDOs.add(subTransferDOByFee);
        }
        //解冻转账单
        SubTransferDO unfreezeFund = this.getSubTransfer(payBaseBO.getPayNo(), payBaseDTO,
                Constant.validateLong(payBaseDTO.getAmt()),payBaseDTO.getPayee(),
                payBaseDTO.getOperator(), payBaseDTO.getCcy());
        unfreezeFund.setFundType(FundTypeEnum.UNFREEZE_TRANSFER.getCode());
        unfreezeFund.setInAccountNo(payBaseDTO.getPayee());
        unfreezeFund.setOutAccountNo(payBaseDTO.getAccountNo());

        subTransferDOs.add(unfreezeFund);
        payBaseBO.setSubTransferDO(subTransferDOs);
        return this;
    }


    /**
     * 获取会计事件号
     *
     * @param eventType     事件类型
     * @param subBizType    产品细类
     * @return              会计事件号
     */
    private String getEventNo(EventTypeEnum eventType, String subBizType){
        EventRuleResBO eventRuleResBO = eventService.queryEventNo(eventType, subBizType);

        if (!PayStatusEnums.SUCCESS.getCode().equals(eventRuleResBO.getStatus())) {
            log.error("会计事件获取失败,事件类型:{},{}",eventType,subBizType);
            throw new BizServiceException(PayCoreErrorCode.EVENT_NO_NOT_EXIST);
        }
        return eventRuleResBO.getEventNo();
    }
    /**
     * 获取手续费资金单
     *
     * @param transferFundDTO   转账资金单
     * @return                  子资金单
     */
    private SubTransferDO getSubTransfer(String payNo, UnfreezeTransferDTO transferFundDTO, long payAmount,
                                         String outAccountNo, String operator, String ccy){
        SubTransferDO subTransferDO = new SubTransferDO();
        subTransferDO.setPayNo(payNo);
        subTransferDO.setFundDetailNo(sequenceFacade.getUniqueSeq());
        subTransferDO.setStatus(PayStatusEnums.INIT.getCode());
        subTransferDO.setPayAmount(payAmount);
        subTransferDO.setCcy(ccy);
        subTransferDO.setOutAccountNo(outAccountNo);
        subTransferDO.setFreezeNo(sequenceFacade.getUniqueSeq());
        subTransferDO.setCreatedBy(operator);
        subTransferDO.setUpdatedBy(operator);

        return subTransferDO;
    }

}
